<?php

/**
 * This is the model class for table "tbl_pelanggan".
 *
 * The followings are the available columns in table 'tbl_pelanggan':
 * @property string $Username
 * @property string $Nama
 * @property string $Alamat
 * @property string $Tanggal_Lahir
 * @property string $Email
 * @property string $Password
 * @property string $Telepon
 *
 * The followings are the available model relations:
 * @property TblMember[] $tblMembers
 * @property TblPesanan[] $tblPesanans
 */
class Pelanggan extends CActiveRecord
{
	
        public $retype_password;
        /**
	 * Returns the static model of the specified AR class.
	 * @return Pelanggan the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_pelanggan';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('Username, Password, retype_password', 'required'),
			array('Username, Password', 'length', 'max'=>10),
			array('Nama, Email', 'length', 'max'=>20),
			array('Alamat', 'length', 'max'=>50),
			array('Telepon', 'length', 'max'=>15),
			array('Tanggal_Lahir', 'safe'),
                        array('Tanggal_Lahir', 'date', 'format'=>'dd-mm-yyyy'),
                        array('retype_password','length', 'max'=>10),
                        array('Password', 'compare', 'compareAttribute'=>'retype_password'),
                        array('retype_password', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('Username, Nama, Alamat, Tanggal_Lahir, Email, Password, Telepon', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'tblMembers' => array(self::HAS_MANY, 'TblMember', 'Username'),
			'tblPesanans' => array(self::HAS_MANY, 'TblPesanan', 'Username'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'Username' => 'Username',
			'Nama' => 'Nama',
			'Alamat' => 'Alamat',
			'Tanggal_Lahir' => 'Tanggal Lahir',
			'Email' => 'Email',
			'Password' => 'Password',
			'Telepon' => 'Telepon',
                        'retype_password' => 'Retype Password',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('Username',$this->Username,true);
		$criteria->compare('Nama',$this->Nama,true);
		$criteria->compare('Alamat',$this->Alamat,true);
		$criteria->compare('Tanggal_Lahir',$this->Tanggal_Lahir,true);
		$criteria->compare('Email',$this->Email,true);
		$criteria->compare('Password',$this->Password,true);
		$criteria->compare('Telepon',$this->Telepon,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

        public function beforeValidate(){
            parent::beforeValidate();
            $valid = !Pelanggan::model()->exists('Username = :id' , array(':id'=>$this->Username));
            $valid = $valid && $this->Username != 'admin'&& $this->Username != 'CSO'&& $this->Username != 'AO'&& $this->Username != 'GM'&& $this->Username != 'SV'&& $this->Username != 'MD';
            if($valid == false)
                $this->addError('Username','Username has been already used');
            return  $valid;
        }

}